package com.ns.sbs.oauth.role.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.current.config.SysConst;
import com.ns.core.utils.sg.ResultError;
import com.ns.core.utils.sg.ResultOk;
import com.ns.sbs.oauth.role.bo.RoleBO;
import com.ns.sbs.oauth.role.bo.RolePageBO;
import com.ns.sbs.oauth.role.dto.RolePageDto;
import com.ns.sbs.oauth.role.entry.RoleEntry;
import com.ns.sbs.oauth.role.service.IRoleService;
import com.ns.sbs.oauth.role.vo.RoleEntryVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 角色相关操作
 *
 * @author lw
 */
@RestController
@RequestMapping("/role/")
@RequiredArgsConstructor
@CrossOrigin
public class RoleController {

    /**
     * 底层Mapper依赖
     */
    private final IRoleService roleService;

    /**
     * 增
     *
     * @param s 参数
     */
    @PostMapping("add")
    @ReqLog(module = "角色模块", name = "增", isRecordFlag = true)
    @CacheEvict(cacheNames = SysConst.ROLE_DICT, key = "'options'")
    @Transactional(rollbackFor = Exception.class)
    public ResultOk add(@RequestBody RoleBO s) {
        RoleEntry role = new RoleEntry();
        BeanUtils.copyProperties(s, role);
        role.setSort(roleService.count() + 1);
        if (roleService.saveOrUpdate(role)) {
            return ResultOk.getSuccessData(s.getId());
        } else {
            throw new ResultError("添加失败");
        }
    }

    /**
     * 删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "角色模块", name = "删", isRecordFlag = true)
    @CacheEvict(cacheNames = SysConst.ROLE_DICT, key = "'options'")
    public ResultOk delete(Long id) {
        return ResultOk.getByBoolean(roleService.removeById(id));
    }

    /**
     * 改
     *
     * @param s 参数
     */
    @PostMapping("update")
    @ReqLog(module = "角色模块", name = "改", isRecordFlag = true)
    @CacheEvict(cacheNames = SysConst.ROLE_DICT, key = "'options'")
    public ResultOk update(@RequestBody RoleBO s) {
        RoleEntry role = new RoleEntry();
        BeanUtils.copyProperties(s, role);
        return ResultOk.getByLine(roleService.update(role));
    }

    /**
     * 改
     *
     * @param id     参数
     * @param status 状态
     */
    @GetMapping("updateStatus")
    @ReqLog(module = "角色模块", name = "改状态", isRecordFlag = true)
    @CacheEvict(cacheNames = SysConst.ROLE_DICT, key = "'options'")
    public ResultOk updateStatus(Long id, Integer status) {
        RoleEntry role = new RoleEntry();
        role.setId(id);
        role.setStatus(status);
        return ResultOk.getByLine(roleService.update(role));
    }

    /**
     * 查
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "角色模块", name = "查")
    public ResultOk getById(Long id) {
        return ResultOk.getSuccessData(roleService.getById(id));
    }

    /**
     * 查 - 集合
     */
    @PostMapping("getList")
    @ReqLog(module = "角色模块", name = "集合")
    public ResultOk getList(@RequestBody RolePageBO bo) {
        RolePageDto dto = new RolePageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<RoleEntryVO> page = roleService.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 查 - 条件选项
     */
    @GetMapping("options")
    @ReqLog(module = "角色模块", name = "集合")
    public ResultOk getOptions() {
        RolePageDto dto = new RolePageDto();
        return ResultOk.getSuccessData(roleService.getOptions(dto));
    }
}
